package com.zhiliao.visitor.demo2;

import java.util.ArrayList;
import java.util.List;

public class VisitorTest {
    public static void main(String[] args) {
        DataStructure dataStructor = new DataStructure();
        dataStructor.add(new EleA());

        dataStructor.accept(new VisitorA());
        dataStructor.accept(new VisitorB());
    }
}

interface Visitor{
    void visitEle1(EleA eleA);
}

class VisitorA implements Visitor{
    @Override
    public void visitEle1(EleA eleA) {
        System.out.println("ele A 被 visitor A 訪問");
        eleA.operation();
    }
}
class VisitorB implements Visitor{
    @Override
    public void visitEle1(EleA eleA) {
        System.out.println("ele A 被 visitor B 訪問");
        eleA.operation();
    }
}

interface Ele{
    void accept(Visitor visitor);
    void operation();
}
class EleA implements Ele{
    @Override
    public void accept(Visitor visitor) {
        visitor.visitEle1(this);
    }

    @Override
    public void operation() {
        System.out.println("visit ele a");
    }
}

class DataStructure {
    List<Ele> eles = new ArrayList<>();

    public void add(Ele ele) {
        eles.add(ele);
    }

    public void accept(Visitor visitor) {
        for (Ele ele : eles) {
            ele.accept(visitor);
        }
    }
}